﻿using Convertor.Interface;
using System;
using System.IO;
using System.Windows;
using Wpf.CommonTools.SystemExtension;

namespace Convertor.Plugin.RenameFile
{
    public class Plugin : PluginBase, IPluginLog
    {
        static string[] LanguagePaths = new string[] { @"Convertor.Plugin.RenameFile;component\Assets\Lang\StringResource.zh.xaml", @"Convertor.Plugin.RenameFile;component\Assets\Lang\StringResource.en.xaml" };

        static Plugin()
        {
            foreach (string path in LanguagePaths)
            {
                ResourceDictionary resourceDictionary = new ResourceDictionary();
                resourceDictionary.Source = new Uri(path, UriKind.Relative);
                Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
            }
        }

        #region IPluginInfo 成员

        public override string Name
        {
            get { return UILanguage.FindResource("PluginType_File") + " - " + UILanguage.FindResource("Plugin_RenameFile_Name"); }
        }

        public override string Description
        {
            get { return UILanguage.FindResource("Plugin_RenameFile_Desc"); }
        }

        

        

        #endregion

        #region IConvertParamters 成员

        public override IConvertParamters Output(IConvertParamters content)
        {
            if (content == null || content.OutputContent == null)
            {
                if (Log != null) { Log(this, UILanguage.FindResource("Plugin_Common_Input_Output_Null"), LogTypes.Error); }
                content.StopConvert = true;
                return content;
            }

            if (content.LastInputContent == null)
            {
                if (Log != null) { Log(this, UILanguage.FindResource("Plugin_Common_Input_Output_Null"), LogTypes.Error); }
                content.StopConvert = true;
                return content;
            }

            string path = content.LastInputContent.ToString().Trim();
            string newPath = content.OutputContent.ToString().Trim();
            if (path != newPath)
            {
                try
                {

                    DirectoryInfo info = new DirectoryInfo(newPath);

                    if (File.Exists(path))
                    {
                        File.Move(path, newPath);
                    }
                    else if (Directory.Exists(path))
                    {
                        Directory.Move(path, newPath);
                    }
                }
                catch (Exception ex)
                {
                    if (Log != null) { Log(this, ex.Message, LogTypes.Error); }
                }
                if (Log != null) { Log(this, string.Format(UILanguage.FindResource("Plugin_RenameFile_ConvertSuccess"), path, newPath), LogTypes.Normal); }
            }

            return new ConvertParamters(content.LastInputContent, content.OutputContent);
        }

        #endregion

    }
}
